/**
 * 迭代器功能测试
 * 在浏览器控制台中运行以验证功能
 */

import { IteratorFactory } from './data-iterators.js'

// 测试数据
const testDoctors = [
  { id: 1, name: '张医生', rating: 4.5, consultation_fee: 200, department: { name: '心内科' }, specialties: ['高血压', '心律失常'] },
  { id: 2, name: '李医生', rating: 3.8, consultation_fee: 150, department: { name: '骨科' }, specialties: ['骨折', '关节炎'] },
  { id: 3, name: '王医生', rating: 4.2, consultation_fee: 300, department: { name: '心内科' }, specialties: ['冠心病', '心衰'] },
  { id: 4, name: '赵医生', rating: 4.8, consultation_fee: 250, department: { name: '神经科' }, specialties: ['头痛', '失眠'] }
]

const testAppointments = [
  { id: 1, appointment_no: 'A001', status: 'confirmed', appointment_date: '2023-12-01T10:00:00', doctor_name: '张医生' },
  { id: 2, appointment_no: 'A002', status: 'pending', appointment_date: '2023-12-02T14:00:00', doctor_name: '李医生' },
  { id: 3, appointment_no: 'A003', status: 'completed', appointment_date: '2023-11-30T09:00:00', doctor_name: '王医生' },
  { id: 4, appointment_no: 'A004', status: 'confirmed', appointment_date: '2023-12-03T16:00:00', doctor_name: '赵医生' }
]

// 测试函数
export function runIteratorTests() {
  console.log('🚀 开始迭代器功能测试...\n')

  // 测试1: 医生过滤迭代器
  console.log('测试1: 医生过滤迭代器')
  const doctorIterator = IteratorFactory.createDoctorIterator(testDoctors, {
    minRating: 4.0
  })

  const highRatedDoctors = []
  for (const doctor of doctorIterator) {
    highRatedDoctors.push(doctor)
  }
  console.log(`✅ 找到 ${highRatedDoctors.length} 位高评分医生:`, highRatedDoctors.map(d => d.name))

  // 测试2: 分页迭代器
  console.log('\n测试2: 分页迭代器')
  const paginationIterator = IteratorFactory.createPaginatedIterator(testDoctors, 2)
  
  let pageCount = 0
  let result = paginationIterator.next()
  while (!result.done) {
    pageCount++
    console.log(`✅ 第 ${result.value.page} 页:`, result.value.data.map(d => d.name))
    result = paginationIterator.next()
  }
  console.log(`总共 ${pageCount} 页`)

  // 测试3: 预约分组迭代器
  console.log('\n测试3: 预约分组迭代器')
  const appointmentIterator = IteratorFactory.createAppointmentIterator(testAppointments, 'status')
  
  let result3 = appointmentIterator.next()
  while (!result3.done) {
    const group = result3.value
    console.log(`✅ ${group.groupName} 组: ${group.count} 个预约`)
    result3 = appointmentIterator.next()
  }

  // 测试4: 过滤迭代器
  console.log('\n测试4: 过滤迭代器')
  const filteredIterator = IteratorFactory.createFilteredIterator(
    testDoctors,
    doctor => doctor.consultation_fee >= 200
  )

  const expensiveDoctors = []
  let result4 = filteredIterator.next()
  while (!result4.done) {
    expensiveDoctors.push(result4.value.item.name)
    result4 = filteredIterator.next()
  }
  console.log(`✅ 费用≥200的医生:`, expensiveDoctors)

  console.log('\n🎉 所有测试完成！')
  
  return {
    success: true,
    results: {
      highRatedDoctors: highRatedDoctors.length,
      totalPages: pageCount,
      expensiveDoctors: expensiveDoctors.length
    }
  }
}

// 简化的测试运行器
export function quickTest() {
  try {
    const results = runIteratorTests()
    console.log('✅ 迭代器测试通过！', results)
    return true
  } catch (error) {
    console.error('❌ 迭代器测试失败:', error)
    return false
  }
}

// 如果在浏览器控制台中运行，自动执行测试
if (typeof window !== 'undefined') {
  // 将测试函数添加到全局对象，方便在控制台调用
  window.testIterators = runIteratorTests
  window.quickTestIterators = quickTest
  
  console.log('🔧 迭代器测试已加载！')
  console.log('在控制台运行: testIterators() 或 quickTestIterators()')
} 